<?php

class GroupMember extends DataObject
{
    // Declare data attribute
    protected $id;
    protected $grouprollid;         //oint
    protected $groupid;                //
    protected $userid;                  //int 
    protected $leader;                  //bool
    
    protected $group;
    protected $user;
    protected $grouproll;

    public function fetchFromDB($id, $id2=null)
    {
        // Obtain the database connection
        $db = DBConnection::inst();
        
        // Name of stored procedure
        $proc = 'GroupMemberFetch';
        $params = array($id);
        if ($db->executeStoredProcedure($proc, $params, $returnData)){
            $this->id = intval($id);    
            $this->grouprollid = intval($returnData[0]["grouprollid"]);
            $this->groupid = intval($returnData[0]["groupid"]);
            $this->userid = intval($returnData[0]["userid"]);
            $this->leader = (bool)intval($returnData[0]["leader"]);

            // Stored procedure succeeded, return true
            return true;
        }
        // Stored procedure failed, return false
        return false;
    }
    
    public function commit()
    {
        // Obtain the database connection
        $db = DBConnection::inst();
        
        $proc = 'GroupMemberCommit';
        $params = Array(
            $this->id,
            $this->leader,
            $this->grouprollid
            );
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            return true;
        }
        return false;
    }
    
    public function delete()
    {
        $db = DBConnection::inst(); 
        $proc = 'GroupMemberDelete';
        $params = array($this->id);
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            return true;
        }
        return false;
    }
    
    public function create()
    {
        if (! isset($this->id))
        {
            // No database object stored.
            $db = DBConnection::inst();
            $proc = 'GroupMemberCreate';
            $params = Array(
                $this->groupid,
                $this->userid,
                $this->leader,
                $this->grouprollid
            );
            
            // Sets the ID to the return value of the stored procedure, which
            // is the ID of the newly created objects.
            if ($this->id = $db->executeStoredProcedure($proc, $params, $returnData))
            {
                return true;
            }
            return false;
        }
        // data object already set
        return false;
    }
    
    public function link()
    {
        if (isset($this->group) && isset($this->grouproll) && isset($this->user))
        {
            return true;
        }
        $DOM = DataObjectManager::inst();
        if (!(isset($this->grouprollid) && isset($this->groupid) && isset($this->userid)))
            return false;
        $this->user = $DOM->fetchDataObject("User", $this->userid);
        $this->user->link();
        $this->grouproll = $DOM->fetchDataObject("GroupRoll", $this->grouprollid);
        $this->grouproll->link();
        $this->group = $DOM->fetchDataObject("Group", $this->groupid);
        $this->group->link();
        return true;
    }
}

?>
